//
//  MVAllKindsClassifyViewController.m
//  NewMusic
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "MVAllKindsClassifyViewController.h"
#import "MVAllKindsClassifyCollectionViewCell.h"
#import "MVAllKindModel.h"
#import "MVAllKindsCollectionReusableView.h"
#import "TheNewMVViewController.h"
@interface MVAllKindsClassifyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property(nonatomic, retain) NSMutableArray *dataArray;
@end

@implementation MVAllKindsClassifyViewController
- (void)dealloc {
    [_titleString release];
    [_dataArray release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [NSMutableArray array];
    //创建CollectionView
    [self createCollectionView];
    //获取数据
    [self loadData];
}
#pragma mark 获取数据
- (void)loadData {
     [CustomerNetworking customerConnectWithURL:@"http://api.dongting.com/song/video/type?app=ttpod&v=v8.1.1.2015110616" andParameters:nil andBlock:^(id response) {
         for (NSDictionary *dic in [response valueForKey:@"data"]) {
             MVAllKindModel *model = [[MVAllKindModel alloc]initWithDictionary:dic];
             [self.dataArray addObject:model];
             [model release];
         }
         [_collectionView reloadData];
     }];
}
#pragma mark 创建CollectionView
- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    //注册cell的重用标识
    [_collectionView registerClass:[MVAllKindsClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"MVAllKindsClassifyCollectionViewCellIdentify"];
    //注册头视图
    [_collectionView registerClass:[MVAllKindsCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self.view addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
}
#pragma mark Cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MVAllKindsClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MVAllKindsClassifyCollectionViewCellIdentify" forIndexPath:indexPath];
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 15;
    if (indexPath.section == 0) {
        cell.nameString = @"全部";
        if ([_titleString isEqualToString:@"全部"]) {
            cell.layer.borderWidth = 2;
            cell.layer.borderColor = [UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0].CGColor;
        }
    }else {
    if (_dataArray.count != 0) {
        if ([_dataArray[indexPath.section - 1] subType].count != 0) {
            cell.typeModel = [[_dataArray[indexPath.section - 1]subType] objectAtIndex:indexPath.item];
            if ([_titleString isEqualToString:[[[_dataArray[indexPath.section - 1]subType] objectAtIndex:indexPath.item] name]]) {
                cell.layer.borderWidth = 2;
                cell.layer.borderColor = [UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0].CGColor;
            }
        }
    }
    }
    return cell;
}
#pragma mark 头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        MVAllKindsCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        if (indexPath.section != 0) {
            if (_dataArray.count != 0) {
                view.titleString = [[_dataArray objectAtIndex:indexPath.section - 1] name];
            }
        }
        return view;
    }else {
        return nil;
    }
}
#pragma mark cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
    if (_dataArray.count != 0) {
        if ([_dataArray[section - 1] subType].count != 0) {
            return [_dataArray[section - 1] subType].count ;
        }else {
            return 0;
        }
    }else {
        return 0;
    }
    }
}
#pragma mark 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _dataArray.count ? _dataArray.count + 1 : 0;
}
#pragma mark cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(self.view.frame.size.width - 10, self.view.frame.size.height / 10);
    }else {
    return CGSizeMake((self.view.frame.size.width - 50) / 4, self.view.frame.size.height / 15);
    }
}
#pragma mark 头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 0);
    }else {
    return CGSizeMake(self.view.frame.size.width, 40);
    }
}
#pragma mark cell的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark 最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}
#pragma mark cell的四周间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}
#pragma mark cell的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.layer.borderWidth = 2;
    cell.layer.borderColor = [UIColor colorWithRed:56 / 255.0 green:192 / 255.0 blue:107 / 255.0 alpha:1.0].CGColor;
    MVTypeModel *model = [MVTypeModel shareMVTypeModel];
    if (indexPath.section == 0) {
        model.name = @"全部";
        model.ID = @"0";
    }else {
        model.name = [[[_dataArray[indexPath.section - 1]subType] objectAtIndex:indexPath.item] name];
        model.ID = [[[_dataArray[indexPath.section - 1]subType] objectAtIndex:indexPath.item] ID];
    }
    [self dismissViewControllerAnimated:YES completion:^{
            }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
